package com.blueocean_health.care.management.controller;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.blueocean_health.care.management.common.base.em.TimeType;
import com.blueocean_health.care.management.common.base.time.TimeUtils;
import com.blueocean_health.care.management.common.excel.ExcelUtil;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.dto.ManyToManyPercentagesDto;
import com.blueocean_health.care.management.service.ManyToManyPercentagesService;

/**
 * 多对多提成报表接口  2018年5月25日16:24:20
 * @author wuyanli
 *
 */
@RestController
@RequestMapping("web/manytomany")
public class ManyToManyPercentagesController{

	@Resource
	private ManyToManyPercentagesService manyService;
	/**
	 * 多对多提成报表  2018年5月25日16:24:38
	 * @return
	 */
	@RequestMapping("percentages/list")
	public SysResult getPercentagesList(@RequestBody Map<String,String> map){
		return manyService.manyToManyTable(map);
		
	}
	
	/**
	 * 导出多对多提成表 Excel  2018年6月1日16:41:44
	 * @param map
	 * @return
	 */
	@RequestMapping("percentages/excel")
	public void getPercentagesListExcel(HttpServletRequest request,HttpServletResponse response){
		try{
			Map<String,String> map = new HashMap<>();
			String startTime = request.getParameter("startTime");
			String endTime = request.getParameter("endTime");
			String groupId = request.getParameter("groupId");
			String searchKey = request.getParameter("searchKey");
			map.put("startTime", startTime);
			map.put("endTime", endTime);
			map.put("groupId", groupId);
			map.put("searchKey", searchKey);
			List<ManyToManyPercentagesDto> percentagesExcel = manyService.getPercentagesExcel(map);
			String finalFileName = URLEncoder.encode("多对多提成表_"+TimeUtils.getTime(TimeType.yyyyMMddHHmmssSSS)+".xls", "UTF8");
			response.addHeader("Content-Disposition", "attachment;filename=" + finalFileName);
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			
			ExcelUtil.getInstance().exportObj2Excel(toClient, percentagesExcel, ManyToManyPercentagesDto.class);
		}catch(IOException e){
			e.printStackTrace();
		}
		
	}
}
